package indi.zhifa.study2025.class001.eltest.controller;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import indi.zhifa.core.common.web.entity.ann.ZhiFaRestController;
import indi.zhifa.study2025.class001.eltest.entity.ElItemEntity;
import indi.zhifa.study2025.commondata.config.ElasticSearchClientProvider;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;

@Slf4j
@Validated
@RequiredArgsConstructor
@RequestMapping(value = "/api/test/el-test")
@ZhiFaRestController
@Tag(name = "el-测试")
public class EsTestController {

    private final ElasticSearchClientProvider mElasticSearchClientProvider;

    @GetMapping("/simpleTest")
    public List<ElItemEntity> elTest() throws IOException {
        ElasticsearchClient elasticsearchClient = mElasticSearchClientProvider.get();
        Reader queryJson = new StringReader(
    """
        {
            "query": {
                "bool": {
                    "filter": [
                      {"term": {"name": "裙"}},
                      {"term": {"tag": "白色"}}
                    ]
                }
            }
        }
       """
        );

        SearchRequest searchRequest = SearchRequest.of(i->i.index("item").withJson(queryJson));
        SearchResponse<ElItemEntity> response = elasticsearchClient.search(searchRequest, ElItemEntity.class);
        List<ElItemEntity> rtn = response.hits().hits().stream().map(hit->hit.source()).toList();
        return rtn;
    }

}
